////
/// Property
/// @group Property
////

//  th-property()
/// Return heading list property value.
///
/// @since 0.0.10
/// @return {number} Heading property value.
///
/// @param {list | string} $heading - A heading map key or list.
/// @param {string} $property-name - (font-size | line-height | margin-top | margin-bottom | breakpoint) A heading property name.
/// @param {number} $breakpoint [false] - A heading list breakpoint.
/// @param {boolean | string} $convert [true] - (true | false | px | em | rem | rel | percent) If returned value should be unit converted or overridden.
/// @param {number} $base-font-size [$th-base-font-size] - Font size used for relative calculations.
///
/// @example scss - Return font size from a h1 heading map.
///   th-property(
///     $heading: h1,
///     $property-name: font-size
///   )
/// @example scss - Return font size from a h1 heading map with a breakpoint of 768px.
///   th-property(
///     $heading: h1,
///     $property-name: font-size,
///     $breakpoint: 768px
///   )
/// @example scss - Return font size from a h1 heading map without unit conversion.
///   th-property(
///     $heading: h1,
///     $property-name: font-size,
///     $convert: false
///   )
/// @example scss - Return font size from a h1 heading map and unit override to percent.
///   th-property(
///     $heading: h1,
///     $property-name: font-size,
///     $convert: percent
///   )
/// @example scss - Return font size from a h1 with a base font size of 24px.
///   th-property(
///     $heading: h1,
///     $property-name: font-size,
///     $base-font-size: 24px
///   )

@function th-property(
  $property-name,
  $heading,
  $breakpoint: false,
  $convert: true,
  $base-font-size: $th-base-font-size
){
  $heading: th-heading($heading, $breakpoint);
  $property: _th-property-get($heading, $property-name);
  @if $property-name != font-size {
    $base-font-size: _th-property-base($heading, $breakpoint, $base-font-size);
  }
  @if $property == default or $property == false {
    $property: _th-property-default($property-name, $base-font-size, false);
  }
  @if string == type-of($convert) {
    $property: th-unit-convert($convert, $property, $base-font-size);
  } @elseif $convert == true {
    $property: _th-property-unit-convert-property($property-name, $property, $base-font-size);
  }
  @return $property;
}

//  _th-property-get()
/// Return heading list property value.
///
/// @since 0.0.11
/// @access private
/// @return {number} The heading property value.
///
/// @param {list} $heading - A heading list.
/// @param {string} $property-name - (font-size | line-height | margin-top | margin-bottom | breakpoint) A heading property name.
///
/// @example scss - Return font size from a heading.
///   _th-property-get(h1, font-size)

@function _th-property-get(
  $heading,
  $property-name
){
  $property-indexes: th-list-get-index-deep($_th-core-heading-structure, $property-name);
  @return th-list-get-nth-deep($heading, $property-indexes);
}

//  _th-property-default()
/// Return default property value.
///
/// @since 0.0.10
/// @access private
/// @return {number} The default property value.
///
/// @param {string} $property-name - (font-size | line-height | margin-top | margin-bottom | breakpoint) A property name.
/// @param {boolean | string} $convert [true] - (true | false | px | em | rem | rel | percent) If returned value should be unit converted or overridden.
/// @param {number} $base-font-size [$th-base-font-size] - Font size used for relative calculations.
///
/// @example scss - Get default font size.
///   _th-property-default(font-size)

@function _th-property-default(
  $property-name,
  $convert: true,
  $base-font-size: $th-base-font-size
){
  $property: map-get($th-property-defaults, $property-name);
  @if $convert == true {
    $property: _th-property-unit-convert-property($property-name, $property, $base-font-size);
  }
  @return $property;
}

//  _th-property-unit-convert-property()
/// Returns a property's converted value.
///
/// @since 0.0.11
/// @access private
/// @return {number} The properties converted value.
///
/// @param {string} $property-name - (font-size | line-height | margin-top | margin-bottom | breakpoint) A property name.
/// @param {number} $property-value - The number value of the property.
/// @param {number} $base-font-size [$th-base-font-size] - Font size used for relative calculations.
///
/// @example scss - Convert heading font size of 14px.
///   _th-property-unit-convert-property(
///     $property-name: font-size,
///     $property-value: 14px
///   )
/// @example scss - Convert heading font size of 14px with base font size of 20px.
///   _th-property-unit-convert-property(
///     $property-name: font-size,
///     $property-value: 14px,
///     $base-font-size: 20px
///   )

@function _th-property-unit-convert-property(
  $property-name,
  $property-value,
  $base-font-size: $th-base-font-size
){
  $unit: map-get($th-property-units, $property-name);
  @return th-unit-convert($unit, $property-value, $base-font-size);
}

//  _th-property-base
/// Get the base font size of a heading if base font size is equal to $th-base-font-size.
///
/// @since 0.0.14
/// @access private
/// @return {number} Property's base font size.
///
/// @param {list | string} $heading - A heading map key or list.
/// @param {number | boolean} $breakpoint [false] -A heading list breakpoint.
/// @param {number} $base-font-size [$th-base-font-size] - Font size used for relative calculations.
///
/// @example scss - Get base font size if $th-base-font-size IS NOT 20px.
///   // $th-base-font-size: 30px
///   _th-property-base(h1, 768px, 20px)
///   // 20px
/// @example scss Get base font size if $th-base-font-size IS 20px.
///   // $th-base-font-size: 20px
///   _th-property-base(h1, 768px, 20px)
///   // 50px

@function _th-property-base(
  $heading,
  $breakpoint,
  $base-font-size
){
  $_return: $base-font-size;
  @if $base-font-size == $th-base-font-size {
    $_return: th-property(
      $property-name: font-size,
      $heading: $heading,
      $breakpoint: $breakpoint,
      $convert: false
    );
  }
  @return $_return;
}

//  th-p()
/// Alias to th-property() function.
///
/// @since 0.0.15

@function th-p(
  $property-name,
  $heading,
  $breakpoint: false,
  $convert: true,
  $base-font-size: $th-base-font-size
){
  @return th-property(
    $property-name: $property-name,
    $heading: $heading,
    $breakpoint: $breakpoint,
    $convert: $convert,
    $base-font-size: $base-font-size
  );
}